﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HammerMUD.Accounts;
using System.Xml.Serialization;
using System.IO;
using HammerMUD.Races;
using System.Security.AccessControl;
using HammerMUD.Areas;
using HammerMUD.Rooms;
using HammerMUD.Items;
using HammerMUD.NPC;

namespace HammerMUD.System
{
    /// <summary>
    /// Contains all of the data I/O for the system
    /// </summary>
    public class SystemUtilities
    {
        public static List<Area> LoadAreas()
        {
            FileStream stream = null;
            try
            {
                stream = new FileStream("areas.xml", FileMode.Open);
                XmlSerializer serializer = new XmlSerializer(typeof(List<Area>));
                List<Area> areas = (List<Area>)serializer.Deserialize(stream);
                stream.Close();

                foreach (Area a in areas)
                {
                    foreach(Room r in a.Rooms)
                    {
                        r.MemberOfArea = a;

                        foreach (Npc n in r.Npcs)
                        {
                            n.CurrentRoom = r;
                        }
                    }
                }

                return areas;
            }
            catch
            {
                if (stream != null)
                {
                    stream.Close();
                }

                Console.WriteLine("Error loading areas");
                return new List<Area>();
            }
        }

        public static void SaveAreas(List<Area> areas)
        {
            FileStream stream = null;
            try
            {
                stream = new FileStream("areas.xml", FileMode.Create, FileAccess.ReadWrite);
                XmlSerializer serializer = new XmlSerializer(typeof(List<Area>));
                serializer.Serialize(stream, areas);
                stream.Close();
            }
            catch (Exception e)
            {
                if (stream != null)
                {
                    stream.Close();
                }

                Console.WriteLine("Error saving areas");
            }
        }

        public static List<Race> LoadRaces()
        {
            FileStream stream = null;
            try
            {
                stream = new FileStream("races.xml", FileMode.Open);
                XmlSerializer serializer = new XmlSerializer(typeof(List<Race>));
                List<Race> races = (List<Race>)serializer.Deserialize(stream);
                stream.Close();
                return races;
            }
            catch
            {
                if (stream != null)
                {
                    stream.Close();
                }

                Console.WriteLine("Error loading races");
                return new List<Race>();
            }
        }

        public static void SaveRaces(List<Race> races)
        {
            FileStream stream = null;
            try
            {
                stream = new FileStream("races.xml", FileMode.Create, FileAccess.ReadWrite);
                XmlSerializer serializer = new XmlSerializer(typeof(List<Race>));
                serializer.Serialize(stream, races);
                stream.Close();
            }
            catch
            {
                if (stream != null)
                {
                    stream.Close();
                }

                Console.WriteLine("Error saving races");
            }
        }

        public static List<Account> LoadAccounts()
        {
            List<Account> accounts;
            FileStream stream = null;

            try
            {
                stream = new FileStream("accounts.xml", FileMode.Open);
                XmlSerializer serializer = new XmlSerializer(typeof(List<Account>));
                accounts = (List<Account>)serializer.Deserialize(stream);
                stream.Close();
                return accounts;
            }
            catch
            {
                if (stream != null)
                {
                    stream.Close();
                }

                Console.WriteLine("Error loading accounts");
                return new List<Account>();
            }
        }

        public static void SaveAccounts(List<Account> accounts)
        {
            FileStream stream = null;
            try
            {
                stream = new FileStream("accounts.xml", FileMode.Create);
                XmlSerializer serializer = new XmlSerializer(typeof(List<Account>));
                serializer.Serialize(stream, accounts);
                stream.Close();
            }
            catch(Exception e)
            {
                if (stream != null)
                {
                    stream.Close();
                }

                Console.WriteLine("Error saving accounts");
            }
        }

        public static void SaveSystemInfo(SystemInfo info)
        {
            FileStream stream = null;
            try
            {
                stream = new FileStream("SystemInfo.xml", FileMode.Create);
                XmlSerializer serializer = new XmlSerializer(typeof(SystemInfo));
                serializer.Serialize(stream, info);
                stream.Close();
            }
            catch
            {
                if (stream != null)
                {
                    stream.Close();
                }

                // do nothing
            }
        }

        public static SystemInfo LoadSystemInfo()
        {
            FileStream stream = null;
            try
            {
                stream = new FileStream("SystemInfo.xml", FileMode.Open);
                XmlSerializer serializer = new XmlSerializer(typeof(SystemInfo));
                SystemInfo info = (SystemInfo)serializer.Deserialize(stream);
                stream.Close();
                return info;
            }
            catch
            {
                if (stream != null)
                {
                    stream.Close();
                }

                return new SystemInfo();
            }
        }

        internal static List<global::HammerMUD.Items.Item> LoadItems()
        {
            FileStream stream = null;
            try
            {
                stream = new FileStream("items.xml", FileMode.Open);
                XmlSerializer serializer = new XmlSerializer(typeof(List<Item>));
                List<Item> items = (List<Item>)serializer.Deserialize(stream);
                stream.Close();
                return items;
            }
            catch
            {
                if (stream != null)
                {
                    stream.Close();
                }

                Console.WriteLine("Error loading items");
                return new List<Item>();
            }
        }

        public static void SaveItems(List<Item> items)
        {
            FileStream stream = null;
            try
            {
                stream = new FileStream("items.xml", FileMode.Create, FileAccess.ReadWrite);
                XmlSerializer serializer = new XmlSerializer(typeof(List<Item>));
                serializer.Serialize(stream, items);
                stream.Close();
            }
            catch
            {
                if (stream != null)
                {
                    stream.Close();
                }

                Console.WriteLine("Error saving items");
            }
        }

        internal static List<Npc> LoadNpcs()
        {
            FileStream stream = null;
            try
            {
                stream = new FileStream("npcs.xml", FileMode.Open);
                XmlSerializer serializer = new XmlSerializer(typeof(List<Npc>));
                List<Npc> npcs = (List<Npc>)serializer.Deserialize(stream);
                stream.Close();
                return npcs;
            }
            catch
            {
                if (stream != null)
                {
                    stream.Close();
                }

                Console.WriteLine("Error loading items");
                return new List<Npc>();
            }
        }

        public static void SaveNpcs(List<Npc> npcs)
        {
            FileStream stream = null;
            try
            {
                stream = new FileStream("npcs.xml", FileMode.Create, FileAccess.ReadWrite);
                XmlSerializer serializer = new XmlSerializer(typeof(List<Npc>));
                serializer.Serialize(stream, npcs);
                stream.Close();
            }
            catch
            {
                if (stream != null)
                {
                    stream.Close();
                }

                Console.WriteLine("Error saving items");
            }
        }
    }
}
